///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	eTBookmarkUI.m
//	SUMMARY:	Implementation of the inspector of Bookmarks
//	SUPERCLASS:	Object
//	INTERFACE:	eTBookmarkUI.nib
//	PROTOCOLS:	<Inspectable>
//	AUTHOR:		Rohit Khare
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//  IMPLEMENTATION COMMENTS
//		You name it.
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	11/13/94:	Modified to use a storage for sorted bookmark lists.
//	05/08/94:	Created. First actual implementation.
///////////////////////////////////////////////////////////////////////////////

#import "eTBookmarkUI.h"
#define PROP "Anchor Properties"

@implementation eTBookmarkUI
//	id	contentPanel;
//	id	contentView;
//	id	theBookmark;
//	long docID;

+ new
{
	static eTBookmarkUI *ui = nil;
	
	if (!ui) {
		ui = [[eTBookmarkUI alloc] init];
	}
	return ui;
}

- init
{
	char        buf[MAXPATHLEN];
	NXBundle   *bundle;

	[super init];
	bundle = [NXBundle bundleForClass:[eTBookmarkUI class]];
	if ( [bundle getPath:buf forResource:"eTBookmarkUI" ofType:"nib"] ) {
		[NXApp loadNibFile:buf owner:self withNames:NO];
	} else {
		NXLogError("NIB not found: eTBookmarkUI");
	}
	contentView = [contentPanel contentView];
	bmStore = [[Storage alloc] init];
	return self;
}
- free {return self;}

- setBookmark:newBookmark inDoc:(long)newDocID
{
	if ((theBookmark != newBookmark) || 
		(docID != newDocID) || 
		([[eTBookmarkBinder new] isDirty])){
		theBookmark = newBookmark;
		docID = newDocID;
		[self load];
	}
	return self;
}

- load
{
	char idstr[32];
	
	sprintf(idstr, "%x", [theBookmark id]);
	[idField setStringValue:NXUniqueString(idstr)];
	[titleField setStringValue:[theBookmark title]];	
	[conditionField setStringValue:[theBookmark condition]];
	[bmStore empty];
	[[eTBookmarkBinder new] getBookmarks:bmStore inDoc:docID];
	[browser loadColumnZero];
	[browser setPath:[theBookmark title]];	
	return self;
}

//////////////////////
- (const NXAtom *) types
{
	static NXAtom types[2] = {NULL, NULL};
	
	if (!types[0]) {
		types[0] = NXUniqueString(PROP);
	}
	return types;
}

- (const char *) inspectorTitle
{
	return NXUniqueString("Bookmark");
}

- resignInspector: (View *) oldInspector ofType: (const char *) type
{
	return self;
}

- activateInspector: (View *) newInspector ofType: (const char *) type
{
	[[NXApp inspector] makeFirstResponder:browser];
	return self;
}

- inspectorForType:(const char *) type
{
	if(!strcmp(type,NXUniqueString(PROP)))
		return contentView;
	NXLogError("Massive Inspector Failure: Asked eTBookmarkUI for: %s", type);
	return contentView;
}

///////////////////////////////

- resetTitle:sender
{
	[theBookmark setTitle:[sender stringValue]];
	[self load];
	return self;
}

- resetCondition:sender
{
	[theBookmark setCondition:[sender stringValue]];
	[self load];
	return self;
}

static int eTBookmarkUI_docompare(eTBookmark **x, eTBookmark **y)
{
	return strcasecmp([*(eTBookmark **)x title],[*(eTBookmark **)y title]);
}

- (int) browser:theBrowser fillMatrix:theMatrix inColumn:(int) col 
{
	int		rows,i;
	id		cell;
	
	rows = [bmStore count];
	qsort(bmStore->dataPtr,bmStore->numElements,bmStore->elementSize,eTBookmarkUI_docompare);
	for (i=0; i <rows; i++) {
		[theMatrix addRow];
		cell = [theMatrix cellAt:i :0];
		[cell setStringValueNoCopy:[*(eTBookmark **)[bmStore elementAt:i] title]];
		[cell setLoaded:YES];
		[cell setLeaf:YES];
	}
	return rows;
}

- selectBookmark2:sender
{
	theBookmark = *(eTBookmark **)[bmStore elementAt:[[browser matrixInColumn:0] selectedRow]];
	// highlight the new bookmark
	[theBookmark highlight:self];
	// inspect the new bookmark
	[self load];
	return sender;
}
- selectBookmark:sender
{
	[self perform:@selector(selectBookmark2:) with:sender afterDelay:0 cancelPrevious:YES];
	return self;
}
@end